package org.niux.AutoLoginQQ.ui;

import java.io.InputStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.niux.AutoLoginQQ.common.Constant;

public class About extends Dialog {

	protected Object result;
	protected Shell shell;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public About(Shell parent, int style) {
		super(parent, style);
		setText("SWT Dialog");
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();

		// 窗口居中
		int width = shell.getMonitor().getClientArea().width;
		int height = shell.getMonitor().getClientArea().height;

		int x = shell.getSize().x;
		int y = shell.getSize().y;

		if (x > width) {
			shell.getSize().x = width;
		}

		if (y > height) {
			shell.getSize().y = height;
		}

		shell.setLocation((width - x) / 2, (height - y) / 2);
		
		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setBackground(SWTResourceManager.getColor(173, 216, 230));
		lblNewLabel.setBounds(3, 147, 431, 17);
		lblNewLabel.setText("NiuXQQ群登器");
		
		Label lblVersion = new Label(shell, SWT.NONE);
		lblVersion.setBackground(SWTResourceManager.getColor(173, 216, 230));
		lblVersion.setBounds(3, 170, 431, 17);
		lblVersion.setText("Version：" + Constant.VERSION);
		
		Label lblNewLabel_1 = new Label(shell, SWT.NONE);
		lblNewLabel_1.setBackground(SWTResourceManager.getColor(173, 216, 230));
		lblNewLabel_1.setBounds(3, 193, 431, 17);
		lblNewLabel_1.setText("作者：梁铭显");
		
		Label lblNewLabel_2 = new Label(shell, SWT.NONE);
		lblNewLabel_2.setBackground(SWTResourceManager.getColor(173, 216, 230));
		lblNewLabel_2.setBounds(3, 216, 431, 17);
		lblNewLabel_2.setText("电子邮箱：liangmingxian@gmail.com");
		
		Label lblNewLabel_3 = new Label(shell, SWT.NONE);
		lblNewLabel_3.setBackground(SWTResourceManager.getColor(173, 216, 230));
		lblNewLabel_3.setBounds(3, 239, 431, 17);
		lblNewLabel_3.setText("版权归作者所有，盗版必究");
		
		Label lblNewLabel_4 = new Label(shell, SWT.WRAP);
		lblNewLabel_4.setBackground(SWTResourceManager.getColor(173, 216, 230));
		lblNewLabel_4.setBounds(3, 262, 431, 35);
		lblNewLabel_4.setText("本软件只作为学习之用，不得用去非法用途，请自觉去下载24小时之后删除，由此产生的法律责任与作者无关");
		
		Button btnNewButton = new Button(shell, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.close();
			}
		});
		btnNewButton.setBounds(344, 303, 80, 27);
		btnNewButton.setText("确定");

		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.SYSTEM_MODAL);
		shell.setBackground(SWTResourceManager.getColor(173, 216, 230));
		shell.setSize(440, 368);
		shell.setText("关于");

		InputStream is = this.getClass().getResourceAsStream("icon/ico.png");
		shell.setImage(new Image(Display.getDefault(), is));

		Canvas canvas = new Canvas(shell, SWT.NONE);

		canvas.setBounds(0, 0, 434, 141);

		is = this.getClass().getResourceAsStream("icon/aboutImage.png");
		final Image image = new Image(Display.getDefault(), is);
		canvas.redraw();

		canvas.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				if (image != null) {
					e.gc.drawImage(image, 0, 0);
				}
			}
		});
	}
}
